package edu.udlap.spring2008.is300.finalexam.app.mvc.view;

import edu.udlap.spring2008.is300.finalexam.app.mvc.model.database.filters.AppImageRecordFilterFavorite;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.objects.ImageRecord;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.ModelState;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.view.ViewComponent;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JScrollPane;

/**
 *
 * @author Ruslán Ledesma Garza
 */
public class ViewFavoritesControls extends ViewComponent implements
        ListSelectionListener,
        ActionListener
{
    protected JScrollPane mainPane_;
    protected JPanel mainPanel_ = new JPanel();
    protected JPanel controlsPanel_ = new JPanel();
    protected JList favoritesList_ = new JList();
    protected JButton removeButton_ =
            new JButton("<html>" +
                        "   <img src=\"file:imgs/deletefavorite.png\" align=\"middle\">" +
                        "   Remove from favorites..." +
                        "</html>");
    
    public ViewFavoritesControls()
    {
        mainPane_ = new JScrollPane();
        mainPane_.setAutoscrolls(true);
        mainPane_.setViewportView(favoritesList_);
        Dimension d = new Dimension(350, 60);
        favoritesList_.setMinimumSize(d);
        favoritesList_.setPreferredSize(d);
        
        controlsPanel_.add(removeButton_);
        
        BorderLayout borderLayout = new BorderLayout();
        mainPanel_.setLayout(borderLayout);
        mainPanel_.add(mainPane_, BorderLayout.CENTER);
        mainPanel_.add(controlsPanel_, BorderLayout.SOUTH);
    }
    
    protected void listenToControls()
    {
        favoritesList_.addListSelectionListener(this);
        removeButton_.addActionListener(this);
    }
    
    protected void stopListeningToControls()
    {
        favoritesList_.removeListSelectionListener(this);
        removeButton_.removeActionListener(this);
    }
    
    // <editor-fold desc="overriden & implemented operations...">
    @Override
    public void setInitialState(ModelState modelState)
    {
        favoritesChanged(modelState);
    }
    
    @Override
    public void imageSetChanged(ModelState modelState)
    {
        ///do nothing...
    }

    @Override
    public void selectionChanged(ModelState modelState)
    {
        stopListeningToControls();
        
        ImageRecord imageRecord =
                modelState.getSelectedImageRecord();
        
        favoritesList_.setSelectedValue(imageRecord, true);
        
        listenToControls();
    }
    
    @Override
    public void favoritesChanged(ModelState modelState)
    {

        stopListeningToControls();

        
        ArrayList<ImageRecord> favoriteImages =
            modelState.getFavoriteImageRecords();

        if(favoriteImages.size() == 0)
        {
            favoritesList_.setEnabled(false);
            removeButton_.setEnabled(false);
            
            String [] msg = {"There are no favorites."};
            favoritesList_.setListData(msg);
        }
        else
        {
            favoritesList_.setEnabled(true);
            removeButton_.setEnabled(true);
            
            favoritesList_.setListData(favoriteImages.toArray());
        }
        
        mainPanel_.validate();
        mainPanel_.repaint();
        
        listenToControls();
    }

    @Override
    public void slideShowMode()
    {
        ///do nothing...
    }

    @Override
    public void gridMode()
    {
        ///do nothing...
    }

    @Override
    public Component getComponent()
    {
        return (Component)mainPanel_;
    }

    @Override
    public void valueChanged(ListSelectionEvent e)
    {
        if(controller_ == null)
            return;
        
        Object source = e.getSource();
        
        if(source == favoritesList_)
        {
            if(!favoritesList_.isSelectionEmpty())
            {
                controller_.setImageRecordFilter(
                        new AppImageRecordFilterFavorite(null, true, false));
                controller_.setSelectedImageRecord(
                        ((ImageRecord)favoritesList_.getSelectedValue()).
                        getId());
            }
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        if(controller_ == null)
            return;
        
        Object source = e.getSource();
        
        if(source == removeButton_)
        {
            if(!favoritesList_.isSelectionEmpty())
            {
                controller_.setImageRecordFilter(
                        new AppImageRecordFilterFavorite(null, true, false));
                controller_.setSelectedImageRecord(
                        ((ImageRecord)favoritesList_.getSelectedValue()).
                        getId());
                controller_.toggleFavoriteStatusOfSelectedImageRecord();
            }
        }
    }
    // </editor-fold>
}
